//
//  SignatureViewController.m
//  Lovely Food
//
//  Created by 李经纬 on 15/12/8.
//  Copyright © 2015年 李经纬. All rights reserved.
//

#import "SignatureViewController.h"
#import <AVOSCloud.h>

@interface SignatureViewController ()<UITextViewDelegate>
@property (weak, nonatomic) IBOutlet UITextView *textView;
@property (weak, nonatomic) IBOutlet UILabel *numberiLabel;

@end

@implementation SignatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textView.layer.borderColor = [[UIColor grayColor] CGColor];
    self.textView.layer.borderWidth = 1;
    self.textView.text = self.str;
    self.textView.delegate = self;
    self.textView.textAlignment = NSTextAlignmentLeft | NSTextAlignmentNatural;
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:(UIBarButtonItemStyleDone) target:self action:@selector(rightAction:)];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.numberiLabel.text = [NSString stringWithFormat:@"%ld", 30 - self.textView.text.length];
}

// 根据字数来限制输入
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{

        NSLog(@"%@", NSStringFromRange(range));
        //计算剩下多少文字可以输入
        if(range.location >= 30)
        {
            return NO;
        } else {
            return YES;
        }
    
}


//在这个地方计算输入的字数
- (void)textViewDidChange:(UITextView *)textView
{

    int   existTextNum = (int)self.textView.text.length;
    self.numberiLabel.text =  [NSString stringWithFormat:@"%d", 30 - existTextNum];
}


// 计算文字的个数
//- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
//{
//    int remainTextNum = 30;
//    NSLog(@"%@", NSStringFromRange(range));
//    //计算剩下多少文字可以输入
//    if(range.location >= 30)
//    {
//        remainTextNum = 0;
//        
//        return NO;
//    }
//    else
//    {
//        NSString  * nsTextContent = self.textView.text;
//        int existTextNum = (int)nsTextContent.length;
//        remainTextNum = 30 - existTextNum;
//        self.numberiLabel.text = [NSString stringWithFormat:@"%d", remainTextNum];
//        return YES;
//    }  
//    
//}


- (void)rightAction:(UIBarButtonItem *)sender {
    
    AVUser *current = [AVUser currentUser];
    
    [current setObject:self.textView.text forKey:@"sdasd"];
    [current saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
